package com.ineatconseil.yougo.client.ui.main.application.tabs.requestsMngt;

import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.ineatconseil.yougo.client.cto.RequestCTO;
import com.ineatconseil.yougo.client.cto.RequestTypeCTO;
import com.ineatconseil.yougo.client.i18n.YougoLabelConstants;
import com.ineatconseil.yougo.client.ui.common.cell.ImageActionCell;
import com.ineatconseil.yougo.client.ui.common.constants.YougoConstants;
import com.ineatconseil.yougo.client.ui.common.popup.BasicPopup;
import com.ineatconseil.yougo.client.ui.common.utils.JavascriptObjectUtils;

/**
 * @author Benjamin Barbier - Ineat Conseil
 */
public class RequestsViewImpl extends Composite implements IRequestsView {

	/**
	 * Default column width constant.
	 */
	private static final double DEFAULT_COLUMN_WIDTH = 400;
	private static final Unit   DEFAULT_COLUMN_UNIT  = Unit.PX;
	
	@UiTemplate("RequestsUiBinder.ui.xml")
	interface RequestTypesUiBinder extends UiBinder<HTMLPanel, RequestsViewImpl> {
	}

	private static RequestTypesUiBinder uiBinder = GWT.create(RequestTypesUiBinder.class);

	public RequestsViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	/**
	 * The requests management cell table.
	 */
	@UiField
	CellTable<RequestCTO> requestsTable;
	
	/**
	 * The pager used to change the range of data.
	 */
	@UiField
	SimplePager requestsPager;
	
	private YougoLabelConstants constants = GWT.create(YougoLabelConstants.class);
	
	@Override
	public void fillTable(int pageSize, JsArray<RequestCTO> requests) {
		requestsTable.setWidth("100%", false);
		requestsTable.setPageSize(pageSize);
		requestsTable.setRowData(JavascriptObjectUtils.toList(requests));
		requestsPager.setDisplay(requestsTable);
	}

	/**
	 * Initialise les colonnes du tableau.
	 * @see http://code.google.com/intl/fr-FR/webtoolkit/doc/latest/DevGuideUiCellWidgets.html
	 */
	@Override
	public void initTableColumns(final JsArray<RequestTypeCTO> requestTypes) {
		
		final Column<RequestCTO, String> typeColumn = new Column<RequestCTO, String>(new TextCell()) {
			@Override
			public String getValue(RequestCTO request) {
				for (int i = 0; i < requestTypes.length(); i++) {
					if (request.getTypeId() == requestTypes.get(i).getId()) {
						return requestTypes.get(i).getDescription();
					}
				}
				return "";
			}
		};
		typeColumn.setSortable(true);

		final Column<RequestCTO, String> fromColumn = new Column<RequestCTO, String>(new TextCell()) {
			@Override
			public String getValue(RequestCTO request) {
				return request.getFrom();
			}
		};
		fromColumn.setSortable(true);

		final Column<RequestCTO, String> toColumn = new Column<RequestCTO, String>(new TextCell()) {
			@Override
			public String getValue(RequestCTO request) {
				return request.getTo();
			}
		};
		toColumn.setSortable(true);

		final Column<RequestCTO, String> observationColumn = new Column<RequestCTO, String>(new TextCell()) {
			@Override
			public String getValue(RequestCTO request) {
				return request.getAskComment();
			}
		};
		observationColumn.setSortable(true);

		final Column<RequestCTO, String> replyColumn = new Column<RequestCTO, String>(new TextCell()) {
			@Override
			public String getValue(RequestCTO request) {
				return request.getAnswerComment();
			}
		};
		replyColumn.setSortable(true);

		final Column<RequestCTO, String> stateColumn = new Column<RequestCTO, String>(new TextCell()) {
			@Override
			public String getValue(RequestCTO request) {
				return constants.getString("requestsState" + request.getStatus().substring(0, 1)
						+ request.getStatus().substring(1).toLowerCase());
			}
		};
		stateColumn.setSortable(true);

		final Column<RequestCTO, RequestCTO> removeColumn = new Column<RequestCTO, RequestCTO>(
				new ImageActionCell<RequestCTO>(YougoConstants.ImagePath.TRASH_ICON,
						new ImageActionCell.Delegate<RequestCTO>() {
							@Override
							public void execute(final RequestCTO request) {
								BasicPopup
										.showWarning("Etes vous sur de vouloir supprimer cette demande de congés allant du "
												+ request.getFrom() + " au " + request.getTo() + " ?");
							}
						})) {
			@Override
			public RequestCTO getValue(final RequestCTO request) {
				return request;
			}

			@Override
			public void render(Context context, RequestCTO request, SafeHtmlBuilder sb) {
				if (YougoConstants.RequestState.PENDING.equals(request.getStatus())) {
					super.render(context, request, sb);
				}
			};

			@Override
			public void onBrowserEvent(final Context context, final Element elem, final RequestCTO request,
					final NativeEvent event) {
				if (YougoConstants.RequestState.PENDING.equals(request.getStatus())) {
					super.onBrowserEvent(context, elem, request, event);
				}
			}
		};

		final Column<RequestCTO, RequestCTO> editColumn = new Column<RequestCTO, RequestCTO>(
				new ImageActionCell<RequestCTO>(YougoConstants.ImagePath.EDIT_ICON,
						new ImageActionCell.Delegate<RequestCTO>() {
							@Override
							public void execute(final RequestCTO request) {
								BasicPopup
										.showWarning("Etes vous sur de vouloir modifier cette demande de congés allant du "
												+ request.getFrom() + " au " + request.getTo() + " ?");
							}
						})) {
			@Override
			public RequestCTO getValue(final RequestCTO request) {
				return request;
			}

			@Override
			public void render(final Context context, final RequestCTO request, final SafeHtmlBuilder sb) {
				if (YougoConstants.RequestState.PENDING.equals(request.getStatus())) {
					super.render(context, request, sb);
				}
			};

			@Override
			public void onBrowserEvent(final Context context, final Element elem, final RequestCTO request,
					final NativeEvent event) {
				if (YougoConstants.RequestState.PENDING.equals(request.getStatus())) {
					super.onBrowserEvent(context, elem, request, event);
				}
			}
		};

		this.addColumnOnTable(typeColumn, constants.typeTableTitle());
		this.addColumnOnTable(fromColumn, constants.fromTableTitle());
		this.addColumnOnTable(toColumn, constants.toTableTitle());
		this.addColumnOnTable(observationColumn, constants.observationTableTitle());
		this.addColumnOnTable(replyColumn, constants.replyTableTitle());
		this.addColumnOnTable(stateColumn, constants.stateTableTitle());
		this.addColumnOnTable(editColumn, "", 33, Unit.PX);
		this.addColumnOnTable(removeColumn, "", 33, Unit.PX);
	}
	
	private void addColumnOnTable(final Column<RequestCTO, ?> column, final String title) {
		addColumnOnTable(column, title, DEFAULT_COLUMN_WIDTH, DEFAULT_COLUMN_UNIT);
	}
	
	private void addColumnOnTable(final Column<RequestCTO, ?> column, final String title, final double columnWidth, Unit unit) {
		requestsTable.addColumn(column, title);
		requestsTable.setColumnWidth(column, columnWidth, unit);
	}
	
}
